package com.uft.utils;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import  com.uft.was.config.*;

@SuppressWarnings({ "restriction", "unused" })
public class WasConfigJAXB {
	public static String DEFAULT_INITIAL_HEAPSIZE="128";
	public static String DEFAULT_MAXIMUM_HEAPSIZE="512";
	public static String DEFAULT_MAXIMUM_LOG_BACKUPS="25";
	public static String DEFAULT_MAXIMUM_LOG_ROLLOVER_SIZE="100";
	public static String DEFAULT_NODEAGENT_POLICY="PREVIOUS";
	public static String createInitialServerConfig(String serverName){
		String result="";
		try {
			JAXBContext jc = JAXBContext.newInstance( "com.uft.was.config" );
			ObjectFactory objFactory = new ObjectFactory();
			Xml xml=objFactory.createXml();
			
			
			// Default Information
			InfoDetail info=objFactory.createInfoDetail();
			info.setAppname(serverName);
			info.setRegion("DEV");
			xml.setInformation(info);
			
			// Cluster Details
			ClusterDetail cluster=objFactory.createClusterDetail();
			xml.setCluster(cluster);
			
			
			// JVM process Defintions
			ProcessDefinitionDetails profDef= objFactory.createProcessDefinitionDetails();		
			JVMDetails jvmDetail=objFactory.createJVMDetails();
			jvmDetail.setInitialHeapSize(DEFAULT_INITIAL_HEAPSIZE);
			jvmDetail.setMaximumHeapSize(DEFAULT_MAXIMUM_HEAPSIZE);
			
			jvmDetail.setChangeWorkingDirectory(YesNoDefinition.YES);
			jvmDetail.setEnableVerboseClassLoading(YesNoDefinition.NO);
			jvmDetail.setEnableVerboseGarbageCollection(YesNoDefinition.NO);
			jvmDetail.setClasspath("");
			jvmDetail.setGenericJvmArguments("");
			
			profDef.setJVM(jvmDetail);	
			
			//Logging and Trace configuration
			LoggingDetails loggingDetails=objFactory.createLoggingDetails();
			loggingDetails.setChangeProcessLogs(YesNoDefinition.YES);
			loggingDetails.setChangeServiceLogs(YesNoDefinition.YES);
			loggingDetails.setChangeTraceLogs(YesNoDefinition.YES);
			loggingDetails.setMaxNumberOfBackupFiles(DEFAULT_MAXIMUM_LOG_BACKUPS);
			loggingDetails.setRolloverSize(DEFAULT_MAXIMUM_LOG_ROLLOVER_SIZE);
			loggingDetails.setRolloverType("BOTH");
			profDef.setLogging(loggingDetails);		
			xml.setProcessDefinition(profDef);
			
			// Process Execution
			ProcessExecutionType processExe= objFactory.createProcessExecutionType();
			processExe.setUnmask("002");
			processExe.setRunAsUser("wasadmin");
			processExe.setRunAsGroup("wasadmin");
			xml.setProcessExecution(processExe);
			
			MonitorPolicyType monitorPolicy= objFactory.createMonitorPolicyType();
			monitorPolicy.setNodeRestartState(DEFAULT_NODEAGENT_POLICY);
			xml.setMonitorPolicy(monitorPolicy);
			
			
			
			
			Marshaller m = jc.createMarshaller();
			m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
			CharArrayWriter caw = new CharArrayWriter();
			m.marshal( xml, new PrintWriter(caw) );
			return caw.toString();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static Xml getRootConfigObj(String xml) throws Exception{
		Xml xmlObj=null;
		
		JAXBContext jc = JAXBContext.newInstance( "com.uft.was.config" );
		Unmarshaller um=jc.createUnmarshaller();
		StringReader reader = new StringReader(xml);
		xmlObj=(Xml)um.unmarshal(reader);
		return xmlObj;
	}
	 public static void main(String[] args) throws JAXBException

	 {
		String xml= createInitialServerConfig("TEST");
		System.out.println(xml);
		try {
			Xml xmlObj=getRootConfigObj(xml);
			System.out.println(xmlObj.getInformation().getAppname());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}
